package com.uls.web.controller.lh;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.uls.lh.domain.LhCarFee;
import com.uls.lh.domain.LhCarinfo;
import com.uls.lh.domain.LhDriverinfo;
import com.uls.lh.mapper.LhCarScheduleManagementMapper;
import com.uls.lh.mapper.LhCarinfoMapper;
import com.uls.lh.mapper.LhDriverinfoMapper;
import com.uls.lh.service.ILhCarinfoService;
import com.uls.lh.service.ILhDriverinfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.uls.common.annotation.Log;
import com.uls.common.core.controller.BaseController;
import com.uls.common.core.domain.AjaxResult;
import com.uls.common.enums.BusinessType;
import com.uls.lh.domain.LhCarScheduleManagement;
import com.uls.lh.service.ILhCarScheduleManagementService;
import com.uls.common.utils.poi.ExcelUtil;
import com.uls.common.core.page.TableDataInfo;

/**
 * 车辆调度管理Controller
 * 
 * @author lh
 * @date 2024-11-29
 */
@RestController
@RequestMapping("/lh/management")
public class LhCarScheduleManagementController extends BaseController
{
    @Autowired
    private ILhCarScheduleManagementService lhCarScheduleManagementService;
    @Autowired
    private LhDriverinfoMapper lhDriverinfoMapper;
    @Autowired
    private LhCarinfoMapper lhCarinfoMapper;
    @Autowired
    private LhCarScheduleManagementMapper lhCarScheduleManagementMapper;
    @Autowired
    private ILhDriverinfoService lhDriverinfoService;
    @Autowired
    private ILhCarinfoService lhCarinfoService;

    /**
     * 查询车辆调度管理列表
     */
    @PreAuthorize("@ss.hasPermi('lh:management:list')")
    @GetMapping("/list")
    public TableDataInfo list(LhCarScheduleManagement lhCarScheduleManagement)
    {
        startPage();
        List<LhCarScheduleManagement> list = lhCarScheduleManagementService.selectLhCarScheduleManagementList(lhCarScheduleManagement);
        for (LhCarScheduleManagement management:list){
            Long CarId=management.getCarId();
            Long driveId=management.getDriverId();
            LhCarinfo lhCarinfo= lhCarinfoService.selectLhCarinfoByCarId(CarId);
            LhDriverinfo lhDriverinfo=lhDriverinfoService.selectLhDriverinfoByDriverId(driveId);
            if (lhCarinfo != null && lhDriverinfo != null){
                management.setModel(lhCarinfo.getModel());
                management.setName(lhDriverinfo.getName());
            }
        }
        System.out.println(list);
        return getDataTable(list);
    }

    /**
     * 导出车辆调度管理列表
     */
    @PreAuthorize("@ss.hasPermi('lh:management:export')")
    @Log(title = "车辆调度管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LhCarScheduleManagement lhCarScheduleManagement)
    {
        List<LhCarScheduleManagement> list = lhCarScheduleManagementService.selectLhCarScheduleManagementList(lhCarScheduleManagement);
        ExcelUtil<LhCarScheduleManagement> util = new ExcelUtil<LhCarScheduleManagement>(LhCarScheduleManagement.class);
        util.exportExcel(response, list, "车辆调度管理数据");
    }

    /**
     * 获取车辆调度管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('lh:management:query')")
    @GetMapping(value = "/{scheduleId}")
    public AjaxResult getInfo(@PathVariable("scheduleId") Long scheduleId)
    {
        return success(lhCarScheduleManagementService.selectLhCarScheduleManagementByScheduleId(scheduleId));
    }

    /**
     * 新增车辆调度管理
     */
    @PreAuthorize("@ss.hasPermi('lh:management:add')")
    @Log(title = "车辆调度管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LhCarScheduleManagement lhCarScheduleManagement)
    {
        return toAjax(lhCarScheduleManagementService.insertLhCarScheduleManagement(lhCarScheduleManagement));
    }

    /**
     * 修改车辆调度管理
     */
    @PreAuthorize("@ss.hasPermi('lh:management:edit')")
    @Log(title = "车辆调度管理", businessType = BusinessType.UPDATE)
    @PutMapping("/lh/management/edit")
    public AjaxResult edit(@RequestBody LhCarScheduleManagement lhCarScheduleManagement)
    {
        return toAjax(lhCarScheduleManagementService.updateLhCarScheduleManagement(lhCarScheduleManagement));
    }

    /**
     * 删除车辆调度管理
     */
    @PreAuthorize("@ss.hasPermi('lh:management:remove')")
    @Log(title = "车辆调度管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{scheduleIds}")
    public AjaxResult remove(@PathVariable Long[] scheduleIds)
    {
        return toAjax(lhCarScheduleManagementService.deleteLhCarScheduleManagementByScheduleIds(scheduleIds));
    }
    /**
     * 完成车辆调度管理
     */
    @PreAuthorize("@ss.hasPermi('lh:management:complete')")
    @Log(title = "车辆调度管理", businessType = BusinessType.DELETE)
    @GetMapping(value = "/complete/{scheduleId}")
    public AjaxResult complete(@PathVariable("scheduleId") Long scheduleId)
    {
        LhCarScheduleManagement lhCarScheduleManagement=lhCarScheduleManagementMapper.selectLhCarScheduleManagementByScheduleId(scheduleId);
        LhDriverinfo lhDriverinfo=lhDriverinfoService.selectLhDriverinfoByDriverId(lhCarScheduleManagement.getDriverId());
        lhDriverinfo.setStatus("1");
        lhDriverinfoMapper.updateLhDriverinfo(lhDriverinfo);
        LhCarinfo lhCarinfo=lhCarinfoService.selectLhCarinfoByCarId(lhCarScheduleManagement.getCarId());
        lhCarinfo.setStatus(1L);
        lhCarinfoMapper.updateLhCarinfo(lhCarinfo);
        lhCarScheduleManagement.setStatus(2);
        return toAjax(lhCarScheduleManagementService.updateLhCarScheduleManagement(lhCarScheduleManagement));
    }



}
